function houseManage() {
	var url = "/IMS/house/showList.ht?cid=" + id + "&pcid=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function ownerManage() {
	var url = "/IMS/owner/showList.ht?communityId=" + id + "&pcid=" + pcid;
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

function returnComm() {
	var url = null;
	if (userType != "3") {
		url = '/IMS/community/showList.ht';
		url = url + "?companyId=" + companyNum;
	} else {
		url = '/IMS/community/showBaseInfo.ht';
		url = url + "?cid=" + communityId;
	}
	$.go(url, {
		dataType : 'html',
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data, textStatus) {
			replaceDocument(url, data);
		}
	});
}

$(function() {
	$("#projectType").select2({
		placeholder : "请选择"
	});
	initCompanyWidget("companySelect2", "/IMS/select/getCompanyList.ht");

	$("#select2-companySelect2-container").html(companyName);

	$("#developerSelect2").select2({
		placeholder : "请选择开发商",
		language : "zh-CN",
		ajax : {
			url : '/IMS/select/getDeveloperList.ht',
			dataType : 'json',
			delay : 250,
			data : function(param) {
				return {
					search : param.term,
					page : param.page || 1
				};
			},
			processResults : function(data, param) {
				param.page = data.nextPage;
				return {
					results : data.list,
					pagination : {
						more : data.hasNextPage
					}
				};
			}
		},
		escapeMarkup : function(markup) {
			return markup;
		},
		// minimumInputLength: 1,
		templateResult : formatRepo,
		// 显示查询结果
		templateSelection : formatRepoSelection
	});

	$("#select2-developerSelect2-container").html(developersName);

	setValidate($("#cmnBaseForm"), {
		rules : {
			communityName : {
				required : true,
				remote : { // 验证费项名称是否存在
					type : "POST",
					url : '/IMS/community/isExsit.ht',
					cache : false,
					async : false,
					data : {
						communityName : function() {
							return $("#communityName").val();
						},
						cid : communityInfoCid,
						belongProvince : function() {
							return $("#s_province").val();
						},
						belongCity : function() {
							return $("#s_city").val();
						},
						belongRegion : function() {
							return $("#s_area").val();
						},
						address : function() {
							return $("#address").val();
						},
						pcid : function() {
							return $("#companySelect2").val();
						}
					}
				}
			},
			belongProvince : {
				required : true
			},
			belongCity : {
				required : true
			},
			address : {
				required : true
			},
			pcid : {
				required : true
			},
			householdNum : {
				required : true,
				digits : true,
				maxlength : 10
			},
			doorNum : {
				digits : true,
				maxlength : 1
			}
		},
		messages : {
			communityName : {
				required : "请输入小区",
				remote : "小区记录已存在"
			},
			belongProvince : {
				required : "请选择省份"
			},
			belongCity : {
				required : "请选择城市"
			},
			address : {
				required : "请输入街道楼牌号"
			},
			pcid : {
				required : "请选择物业公司"
			},
			householdNum : {
				required : "请输入总户数",
				digits : "请输入整数",
				maxlength : "范围0到100,000"
			},
			doorNum : {
				digits : "请输入整数",
				maxlength : "范围0到9"
			}
		}
	});

	if (cPicture) {
		//showUploadTip(fileServerUrl + cPicture + "?v=" + new Date().getTime(), "uploadifyButton2");
		$("#uploadifyButton2").attr('src',fileServerUrl + cPicture);
	}
	
	$("#uploadifyButton2").html5Upload({
		url : fileUploadUrl,
		fileTypeExts : [ '.gif', '.jpg', '.png' ],
		onUploadSuccess : function(data, filename) {
			if (data) {
				if (data.code == 1) {
					_alert(data.message);
				} else {
					if (data.result) {
						$("#tempFileName").val(data.result[0]);
					}
				}
			}
		}
	});

	/*
	$("#uploadifyButton2").uploadify({ // 上传小区鸟瞰图
		'id' : 'uploadifyButton2',
		'swf' : '/IMS/js/plugins/file/uploadify/uploadify.swf',
		'uploader' : fileUploadUrl,
		'buttonText' : '',
		'fileTypeDesc' : '当前允许上传格式：*.jpg | *.gif | *.png',
		'fileTypeExts' : '*.gif; *.jpg; *.png',
		'queueID' : 'fileQueue2',
		'auto' : true,
		'multi' : false,
		'width' : 270,
		'height' : 200,
		'method' : 'post',
		'fileSizeLimit' : '10MB',
		'onUploadSuccess' : function(fileData, jsonData) {
			var obj;
			if (jsonData) {
				obj = eval('(' + jsonData + ')');
				if (obj.code == 1) {
					_alert(obj.message);
				} else {
					if (obj.result) {
						$("#uploadifyButton2-button img").remove();
						$("#uploadifyButton2-button").append("<img src='" + obj.result[0] + "'/>");
						$("#tempFileName").val(obj.result[0]);
					}
				}
			}
		}
	});
	*/
	
	initProvinceSeletor(); // 初始化省市区下拉框
	$("#s_province option[value='" + curProvince + "']").attr("selected", "selected");
	var _provinceIndex = $("#s_province option:selected").attr("index");
	if (_provinceIndex && _provinceCache[_provinceIndex]) {
		initCitySelector(_provinceCache[_provinceIndex].children);
	}
	$("#s_city option[value='" + curCity + "']").attr("selected", "selected");
	var _cityIndex = $("#s_city option:selected").attr("index");
	if (_cityIndex)
		initAreaSelector(_provinceCache[_provinceIndex].children[_cityIndex].children);
	else
		initAreaSelector(null);
	$("#s_area option[value='" + curArea + "']").attr("selected", "selected");
	// 居委会
	$('#committeeSelect2').val(bjwh).select2({
		placeholder : committeesFlag ? "请选择" : "尚未录入居委会信息"
	});
	$("input[name=isGood][value=" + isGood + "]").click();

	$("#s_province").change(function() {
		var index = $(this).children("option:selected").attr("index");
		if (index) {
			initCitySelector(_provinceCache[index].children);
		} else {
			initCitySelector(null);
		}

		$("#areaCodes").val($(this).val());

	});

	$("#s_city").change(

	function() {
		var p_index = $("#s_province").children("option:selected").attr("index");
		var index = $(this).children("option:selected").attr("index");
		if (p_index >= 0 && index >= 0) {
			initAreaSelector(_provinceCache[p_index].children[index].children);
		} else {
			initAreaSelector(null);
		}

		$("#areaCodes").val($("#s_province").val() + "," + $(this).val());

	});

	$("#s_area").change(function() {
		$("#areaCodes").val($("#s_province").val() + "," + $("#s_city").val() + "," + $(this).val());
	});

	$(".ks-switchable-nav a").click(function() {
		if (_curCid || $(this).attr("id") == "a_base") {
			return true;
		} else {
			_alert("请填写基础信息并保存后再填写其他信息");
			return false;
		}
	});

	$("#s_province").select2({
		placeholder : "请选择省份"
	});
	$("#s_city").select2({
		placeholder : "请选择城市"
	});
	$("#s_area").select2({
		placeholder : "请选择县区"
	});
});

function initProvinceSeletor() {
	$("#s_province option").remove();
	var citys;
	$.each(_provinceCache, function(index) {
		var optionDom = "<option value='" + this.code + "' ";
		if (index == 0) {
			optionDom += "selected='" + "seleceted' "
		}
		optionDom += "index=" + index + ">" + this.name + "</option>";
		$("#s_province").append(optionDom);
	});
	initCitySelector(citys);
}

function initCitySelector(citys) {
	$("#s_city option").remove();
	var areas;

	$("#s_city").append("<option value='' selected='selected'>请选择</option>");
	if (citys && citys.length > 0) {
		$.each(citys, function(index) {
			var optionDom = "<option value='" + this.code + "' ";
			optionDom += "index=" + index + ">" + this.name + "</option>";
			$("#s_city").append(optionDom);
		});
	}
	$("#s_city").select2({
		placeholder : "请选择城市"
	});
	initAreaSelector(areas);
}

function initAreaSelector(areas) {
	$("#s_area option").remove();
	$("#s_area").append("<option value='' selected='selected'>请选择</option>");
	if (areas && areas.length > 0) {
		$.each(areas, function(index) {
			var optionDom = "<option value='" + this.code + "'>" + this.name + "</option>";
			$("#s_area").append(optionDom);
		});
	}
	$("#s_area").select2({
		placeholder : "请选择区县"
	});
}

function checkNames() {
	var action = $("#baseCid").val() ? '/IMS/community/checkUpdBaseInfo.ht' : '/IMS/community/checkAddBaseInfo.ht';
	$.ajax({
		url : action,
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		data : {
			cid : $("#baseCid").val(),
			communityName : $("#communityName").val(),
			belongProvince : $("#s_province").val(),
			belongCity : $("#s_city").val(),
			belongRegion : $("#s_area").val(),
			pcid : $("#companySelect2").val(),
			did : $("#developerSelect2").val()
		},
		success : function(data) {
			$("#isCheckedNames").val("true");
			if (data.code == "0") {
				_alert("检查通过");
				$("#allowSubmit").val("true");
			} else {
				$("#allowSubmit").val("false");
				_alert("检查失败，请重新填写小区信息");
			}
			hideProccessBar();
		}
	});
}

function saveInfo() {
	var url = '/IMS/community/checkVillageName.ht';
	var communityName = $("#communityName").val();
	var address = $("#address").val();
	var sysTradeArea = $("#sysTradeArea").val();
	var schoolDistrict = $("#schoolDistrict").val();
	var householdNum = $("#householdNum").val();
	var doorNum = $("#doorNum").val();
	var communityDesc = $("#communityDesc").val();

	var belongRegion = $("#s_area").val();

	var _provinceIndex = $("#s_province option:selected").attr("index");
	var _cityIndex = $("#s_city option:selected").attr("index");
	var _region = _provinceCache[_provinceIndex].children[_cityIndex].children;
	if (_region && _region.length > 0) {
		if (Is_Null(belongRegion)) {
			_alert("请选择所属区县");
			return;
		}
	}
	if (communityName.length > 49) {
		_alert("小区超出最大长度！");
		return;
	}
	if (address.length > 49) {
		_alert("地址超出最大长度！");
		return;
	}
	if (sysTradeArea.length > 49) {
		_alert("所属商圈超出最大长度！");
		return;
	}
	if (schoolDistrict.length > 49) {
		_alert("所属学区超出最大长度！");
		return;
	}
	if (parseInt(householdNum) >= 100001) {
		_alert("总户数超出最大长度！");
		return;
	}
	if (parseInt(doorNum) > 10) {
		_alert("大门个数超出最大长度！");
		return;
	}
	if (communityDesc.length > 100) {
		_alert("小区简介超出最大长度！");
		return;
	}

	$.ajax({
		url : url,
		data : {
			belongCity : $("#s_city").val(),
			communityName : $("#communityName").val(),
			cid : $("#baseCid").val(),
			belongRegion : $("#s_area").val()
		},
		beforeSend : function() {
			showProccessBar();
		},
		error : function() {
			hideProccessBar();
		},
		success : function(data) {
			if (data.msgCode == message_success_code) {
				$.submit($("#cmnBaseForm"), {
					dataType : 'json',
					error : function() {
						hideProccessBar();
					},
					success : function(data, textStatus) {
						if (data.code == '0') {
							_alert("保存成功", function() {
								if (userType != 3) {
									if (pcid != null && pcid != "") {
										var url = "/IMS/community/showList.ht?companyId=" + pcid;
										$.go(url, {
											dataType : 'html',
											error : function() {
												hideProccessBar();
											},
											success : function(data, textStatus) {
												replaceDocument(url, data);
											}
										});
									} else {
										var url = '/IMS/community/showList.ht';
										$.go(url, {
											dataType : 'html',
											error : function() {
												hideProccessBar();
											},
											success : function(data, textStatus) {
												replaceDocument(url, data);
											}
										});
									}
								} else {
									var url = "/IMS/community/showBaseInfo.ht?cid=" + id;
									$.go(url, {
										dataType : 'html',
										error : function() {
											hideProccessBar();
										},
										success : function(data, textStatus) {
											replaceDocument(url, data);
										}
									});
								}
							});
						} else {
							_alert("保存失败" + data.message);
							hideProccessBar();
						}
					}
				});
			} else {
				var msg = "您输入的小区" + $("#communityName").val() + "已经在" + $("#s_city  option:selected").text() + "存在了";
				_alert(msg);
				hideProccessBar();
				return;
			}
		}
	});
}